#include <stdio.h>
#include <stdlib.h>

#define TRUE            1
#define FALSE           0
#define ARRAY_MAX_SIZE  30 //最大周期
/*
void input(int* x0,int* mod,int* yinziA,int* yinziC)
{
    printf("请输入X0的值:\n");
    scanf("%d",x0);
    printf("请输入Mod的值(得大于0):\n");
    scanf("%d",mod);
    if( !mod )
    {
        printf("请输入因子A的值(得大于0,小于m):\n");
        scanf("%d",yinziA);
        if( yinziA > 0 && yinziA < mod )
        {
        printf("请输入因子C的值(得大于0,小于m):\n");
        scanf("%d",yinziC);
        }
        else
        {
            printf("输入错误，请输入大于0和小于Mod\n");
            exit(EXIT_FAILURE);
        }
    }
    else
    {
        printf("输入错误，请输入大于0的号\n");
        exit(EXIT_FAILURE);
    }
}
*/

int input(int* x0,int* mod,int* yinziA,int* yinziC)
{
    printf("请输入X0,Mod,因子A,因子C的值(中间以空格为分隔符)：\n");

    scanf("%d %d %d %d",x0,mod,yinziA,yinziC);

    if( (*mod) <= 0  )
    {
        printf("输入错误（mod的值小于零)");
        return FALSE;
    }
    if( (*yinziA) < 0 || (*yinziC) < 0 || (*yinziA) > (*mod) || (*yinziA) > (*mod))
    {
        printf("输入错误（yinziA/yinziC的值小于零或大于Mod)");
        return FALSE;
    }
    return TRUE;
}


int main()
{
    int x0, mod, yinziA, yinziC, i, array_target[ARRAY_MAX_SIZE];
    if( !input(&x0, &mod, &yinziA, &yinziC) )//判断输入
    {
        exit(EXIT_FAILURE);
    }

    array_target[0] = x0;
    for( i = 1; i < mod; i++ )
    {
        array_target[i] = ( yinziA * array_target[i-1] + yinziC ) % mod;
        if( array_target[i] == array_target[0] )
        {
            break;
        }
    }

    printf("{ ");
    for(int k = 0; k < i; k++)
    {
        printf("%d, ",array_target[k]);
    }
    printf("}.");

    return 0;
}
